package com.coincalf.task.event;

import com.alibaba.fastjson.JSONObject;
import com.coincalf.chan.feign.MessagingServiceClient;
import com.coincalf.feign.MarketServiceClient;
import com.coincalf.framework.http.Response;
import com.esotericsoftware.minlog.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 推送实时成交订单数据
 *
 * @author qiang
 */
@Component
@Slf4j
public class TradeEvent implements IEvent {

    @Autowired
    private MarketServiceClient marketServiceClient;
    @Autowired
    private MessagingServiceClient messagingServiceClient;

    @Override
    public void handle() {
        try {
            marketServiceClient.tradeMarkets().forEach(market -> {
                String groupName = String.format("market.%s.trade.detail", market.getSymbol()).toLowerCase();
                Response response = marketServiceClient.trades(market.getSymbol(), 10);
                JSONObject orders = new JSONObject();
                orders.put("data", response.getData());
                messagingServiceClient.convertAndSendToGroup(groupName, orders.toJSONString());
            });
        } catch (Exception e) {
            log.error("推送最新成交订单异常");
            e.printStackTrace();
        }
    }

}
